package com.hfy.admin.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.hfy.admin.service.PostManagementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import result.Result;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @ author 韩
 * time: 2025/1/4 0:09
 */
@RestController
@RequestMapping("/management/post")
@Tag(name = "帖子管理控制器" ,description = "主要对帖子系统做一些管理操作")
public class PostManagementController {

    @Autowired
    private PostManagementService postManagementService;

    @GetMapping("/page")
    @Operation(summary = "分页查询帖子", description = "根据不同条件分页查询")
    @Parameters({
            @Parameter(name = "topicId",description = "筛选条件-话题"),
            @Parameter(name = "title",description = "筛选条件-标题"),
            @Parameter(name = "startTime",description = "筛选条件-开始时间"),
            @Parameter(name = "endTime",description = "筛选条件-截至时间"),
            @Parameter(name = "startLikeNum",description = "筛选条件-点赞量初始"),
            @Parameter(name = "endLikeNum",description = "筛选条件-点赞量截至"),
            @Parameter(name = "startCollectNum",description = "筛选条件-收藏量初始"),
            @Parameter(name = "endCollectNum",description = "筛选条件-收藏量截至"),
            @Parameter(name = "startViewNum",description = "筛选条件-浏览量初始"),
            @Parameter(name = "endViewNum",description = "筛选条件-浏览量截至"),
            @Parameter(name = "startWordNum",description = "筛选条件-字数初始"),
            @Parameter(name = "endWordNum",description = "筛选条件-字数截至"),
            @Parameter(name = "contentKey",description = "筛选条件-搜索内容"),
            @Parameter(name = "isNameless",description = "筛选条件-是否匿名"),
            @Parameter(name = "isDie",description = "筛选条件-是否隐藏"),
    })
    public Result pagePostsByFilters(@RequestParam(value = "pageNum", defaultValue = "0") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                              @RequestParam(value = "title", defaultValue = "10") String title,
                              @RequestParam(value = "topicId", required = false) Integer topicId,
                              @RequestParam(value = "startTime", required = false) LocalDateTime startTime,
                              @RequestParam(value = "endTime", required = false) LocalDateTime endTime,
                              @RequestParam(value = "startLikeNum", required = false) Integer startLikeNum,
                              @RequestParam(value = "endLikeNum", required = false) Integer endLikeNum,
                              @RequestParam(value = "startCollectNum", required = false) Integer startCollectNum,
                              @RequestParam(value = "endCollectNum", required = false) Integer endCollectNum,
                              @RequestParam(value = "startViewNum", required = false) Integer startViewNum,
                              @RequestParam(value = "endViewNum", required = false) Integer endViewNum,
                              @RequestParam(value = "startWordNum", required = false) Integer startWordNum,
                              @RequestParam(value = "endWordNum", required = false) Integer endWordNum,
                              @RequestParam(value = "contentKey", required = false) String contentKey,
                              @RequestParam(value = "isNameless", required = false) Boolean isNameless,
                              @RequestParam(value = "isDie", required = false) Boolean isDie) {
        return postManagementService.pagePostsByFilters(pageNum, pageSize, title, topicId, startTime, endTime, startLikeNum, endLikeNum, startCollectNum, endCollectNum, startViewNum, endViewNum, startWordNum, endWordNum, contentKey, isNameless, isDie);
    }

    // 新增接口 - 精确搜索帖子
    @GetMapping("/search")
    @DS("user")
    @Operation(summary = "精确搜索帖子", description = "根据ID、标题或作者搜索帖子，用于热度管理")
    @Parameters({
            @Parameter(name = "type", description = "搜索类型：id, title, author"),
            @Parameter(name = "value", description = "搜索值"),
            @Parameter(name = "startTime", description = "开始时间"),
            @Parameter(name = "endTime", description = "结束时间")
    })
    public Result searchPosts(
            @RequestParam(value = "type", required = false, defaultValue = "id") String type,
            @RequestParam(value = "value", required = false) String value,
            @RequestParam(value = "startTime", required = false) 
                @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam(value = "endTime", required = false) 
                @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        
        return postManagementService.searchPosts(type, value, startTime, endTime);
    }

    // 批量更新帖子热度
    @PutMapping("/hot/batch")
    @DS("user")
    @Operation(summary = "批量更新帖子热度", description = "根据提供的帖子ID列表和热度值更新帖子热度")
    public Result batchUpdateHot(@RequestBody List<Map<String, Object>> posts) {
        return postManagementService.batchUpdateHot(posts);
    }

    // 重置帖子热度
    @PutMapping("/hot/reset")
    @DS("user")
    @Operation(summary = "重置帖子热度", description = "将指定帖子的热度重置为默认值")
    public Result resetPostsHot(@RequestBody List<Integer> postIds) {
        return postManagementService.resetPostsHot(postIds);
    }
}
